<?php

defined( '_JEXEC' ) or die( 'Restricted access' );

class booking_helper
{
        function DateTime($date, $time)
        {//01-NOV-2008 04:40

            if ((strlen($date)!=11)||(strlen($time)!=5)||($date[2]!="-")||($date[6]!="-")||($time[2]!=":")) return false;

            $temp = substr ($date, 7, 4);	//year
            switch(substr ($date, 3, 3))
            {
                    case "JAN": $temp .= "01"; break;
                    case "FEB": $temp .= "02"; break;
                    case "MAR": $temp .= "03"; break;
                    case "APR": $temp .= "04"; break;
                    case "MAY": $temp .= "05"; break;
                    case "JUN": $temp .= "06"; break;
                    case "JUL": $temp .= "07"; break;
                    case "AUG": $temp .= "08"; break;
                    case "SEP": $temp .= "09"; break;
                    case "OCT": $temp .= "10"; break;
                    case "NOV": $temp .= "11"; break;
                    case "DEC": $temp .= "12"; break;
            }
            $temp.= substr ($date, 0, 2);	//day
            $temp.= substr ($time, 0, 2);	//time
            $temp.= substr ($time, 3, 2);

            if ( is_numeric($temp) )	return $temp;
            else return false;
        }
        
        function DateTime2($date, $time)
        {//	"31/10/2008", "08:45"	echo $date." ".$time."<br>";

            if ((strlen($date)!=10)||(strlen($time)!=5)||($time[2]!=":")) return false;

            $temp = substr ($date, 6, 4);	//year
            $temp.= substr ($date, 3, 2);	//month
            $temp.= substr ($date, 0, 2);	//day
            $temp.= substr ($time, 0, 2);	//hour
            $temp.= substr ($time, 3, 2);	//minutes

            if ( is_numeric($temp) )	return $temp;
            else return false;
        }
        
	function DateTimeHuman($datetime)
        {//	"200809230845"
            if ( (strlen($datetime)==12) && is_numeric($datetime) )
            {
                    $temp = substr ($datetime, 6, 2);	//day date
                    $temp.= "/";
                    $temp.= substr ($datetime, 4, 2);	//month
                    $temp.= "/";
                    $temp.= substr ($datetime, 0, 4);	//year
                    $temp.= " ";

                    $temp.= substr ($datetime, 8, 2);	//hours time
                    $temp.= ":";
                    $temp.= substr ($datetime, 10, 2);	//minutes
                    return $temp;
            }
            else return false;
        }
        
        function getcartype($passengers)
        {
                if ($passengers==0)	return "Saloon";
                elseif ($passengers==1)	return "MPV";
                elseif ($passengers>2)	return "8 seat";
        }

        function getvehicletype($choice)
        {
                if ($choice==0)	return "Saloon";
                elseif ($choice==1)	return "MPV";
                elseif ($choice==2)	return "8 seat";
                else return "";
        }
        
        function getChildSeattype($choice)
        {
                if ($choice==1)	return "Infant seat";
                elseif ($choice==2)	return "Child seat";
                elseif ($choice==3)	return "Booster seat";
                else return "";
        }
        
        function DateHuman($datetime)
        {//	"200809230845"
            if ( (strlen($datetime)==12) && is_numeric($datetime) )
            {
                    $temp = substr ($datetime, 6, 2);	//day	date
                    $temp.= "/";

                    $temp.= substr ($datetime, 4, 2);	//month
                    $temp.= "/";

                    $temp.= substr ($datetime, 0, 4);	//year
                    return $temp;
            }
            else return false;
        }
        
        function TimeHuman($datetime)
        {//	"200809230845"
            if ( (strlen($datetime)==12) && is_numeric($datetime) )
            {
                    $temp = substr ($datetime, 8, 2);	//time
                    $temp.= ":";
                    $temp.= substr ($datetime, 10, 2);
                    return $temp;
            }
            else return false;
        }
        
        function AddressType($from_dest, $part)
        {
                switch ($from_dest)
                {
                        case 0:
                                switch ($part)
                                {
                                        case 1:	$temp = JText::_('FLIGHT');	break;
                                        case 2:	$temp = "From";		break;
                                        case 3:	$temp = "Terminal";	break;
                                }
                                break;
                        case 1:
                                switch ($part)
                                {
                                        /*case 1:	$temp = "Door №";	break;
                                        case 2:	$temp = "Flat №";	break;
                                        case 3:	$temp = "Address";	break;*/
										case 1:	$temp = "Street number and name";	break;
                                        case 2:	$temp = "Town/City";	break;
                                        case 3:	$temp = "Country";	break;
                                }
                                break;
                        case 2:
                                switch ($part)
                                {
                                        case 1:	$temp = "Vessel"; break;
                                        case 2:	$temp = "To";	break;
                                        case 3:	$temp = "Dock";	break;
                                }
                                break;
                }
                return $temp;
        }
        
        function phone()
        {
                /*
                 tel:0044(0)845 4734121
                 mob.:0044(0)7854 516293
                 fax.:0044(0)700 345 1604
                 e-mail:vesconti@gmail.com
                 skype:vesconti
                 new contact phone: 0044(0)845 15589084
                 new 0044 (0) 844 3572661
                 */

                //return "<b><font color='#ef8f0f' size='-2'>+44 (0) 844 357 2661</font></b>";
				return "";
        }
        function repair_format($str)
        {
                //	1/12/2008	<->		01/12/2008	??
                //	21/1/2008	<->		21/01/2008	??
                //	9:23		<->		09:23

                if (strlen($str)<5)
                {
                        if (strpos($str, ":") == 1 ) $str = "0$str";
                        //		if (strlen($str)<5 ) insertstring($str, "0", 3);
                }
                else if ( (strlen($str)>5) && (strlen($str)<10) )
                {
                        if (strpos($str, "/") == 1 ) $str = "0$str";
                        if (strpos($str, "/",3) == 4 ) $str = insertstring($str, "/", "0");
                }

                return $str;
        }
        
        function is_valid_email($email) {
          $result = true;
          if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
            $result = false;
          }
          return $result;
        }
        
        function is_valid_phone($string) {
              if(preg_match('/^(NA|[0-9+-]+)$/',$string)) {
                        return true;
                } else {
                        return false;
                }
        }
        
        function get_place_field($id = 0, $field = 'title'){
                
                $result = '';
                if($id != 0){
                    $db = & JFactory::getDBO();
            
                    $query = 'SELECT `'.$field.'`'
                            . ' FROM #__taxibooking_points'
                            . ' WHERE id = '.$id
                            ;
                    $db->setQuery($query);
                    $db->query();
                    $result = $db->loadResult(); 
                    
                }
                return $result;
        }
        
        function get_route_field($id = 0, $field = 'title'){
                
                $result = '';
                if($id != 0){
                    $db = & JFactory::getDBO();
            
                    $query = 'SELECT `'.$field.'`'
                            . ' FROM #__taxibooking_routes'
                            . ' WHERE id = '.$id
                            ;
                    $db->setQuery($query);
                    $db->query();
                    $result = $db->loadResult(); 
                    
                }
                return $result;
        }
        
        /**
	 * Checks a currency symbol wether it is a HTML entity.
	 * When not and $convertToEntity is true, it converts the symbol
	 *
	 * @param string $symbol
	 */
	function checkCurrencySymbol( $symbol, $convertToEntity=true ) {
		
		$symbol = str_replace('&amp;', '&', $symbol );
		
		if( substr( $symbol, 0, 1) == '&' && substr( $symbol, strlen($symbol)-1, 1 ) == ';') {
			return $symbol;
		}
		else {
			if( $convertToEntity ) {
				$symbol = htmlentities( $symbol, ENT_QUOTES, 'utf-8' );
				
				if( substr( $symbol, 0, 1) == '&' && substr( $symbol, strlen($symbol)-1, 1 ) == ';') {
					return $symbol;
				}		
				// Sometimes htmlentities() doesn't return a valid HTML Entity
				switch( ord( $symbol ) ) {
					case 128:
					case 63:
						$symbol = '&euro;';
						break;
				}
						
			}
		}
		
		return $symbol;
	}
        
        /**
	 * Pulls settings from database and stores in an static object
	 *
	 * @return object
	 * @since 0.9
	 */
	function &config()
	{
		static $config;

		if (!is_object($config))
		{
			$db 	= & JFactory::getDBO();
			$sql 	= 'SELECT * FROM #__taxibooking_settings WHERE id = 1';
			$db->setQuery($sql);
			$config = $db->loadObject();
		}

		return $config;
	}

}
?>